﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class UcBienBanBanGiao : UserControl
    {
        public UcBienBanBanGiao()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        int vitri;
        private void loaddata()
        {
            gridControlBanGiao.DataSource = cn.getDataTable("tblBienBanBanGiao_DS");
        }
        private void UcBienBanBanGiao_Load(object sender, EventArgs e)
        {
            loaddata();
        }

        private void gridViewBanGiao_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {

                txtMaBienBan.Text = gridViewBanGiao.GetFocusedRowCellDisplayText("MaBangiao");
                txtTenToGiao.Text = gridViewBanGiao.GetFocusedRowCellDisplayText("TenToGiao");
                txtToNhan.Text = gridViewBanGiao.GetFocusedRowCellDisplayText("TenToNhan");
                txtNguoiGiao.Text = gridViewBanGiao.GetFocusedRowCellDisplayText("NguoiGiao");
                txtNguoiNhan.Text = gridViewBanGiao.GetFocusedRowCellDisplayText("NguoiNhan");
                txtNgayGiao.Text = gridViewBanGiao.GetFocusedRowCellDisplayText("NgayBanGiao");
                bientinhBienBanBanGiao.Tennguoigiao = gridViewBanGiao.GetFocusedRowCellDisplayText("NguoiGiao");
                bientinhBienBanBanGiao.Tennguoinhan = gridViewBanGiao.GetFocusedRowCellDisplayText("NguoiNhan");
                bientinhBienBanBanGiao.Tentogiao = gridViewBanGiao.GetFocusedRowCellDisplayText("TenToGiao");
                bientinhBienBanBanGiao.Tentonhan = gridViewBanGiao.GetFocusedRowCellDisplayText("TenToNhan");
                bientinhBienBanBanGiao.Mabienban = int.Parse(gridViewBanGiao.GetFocusedRowCellDisplayText("MaBangiao").ToString());
                bientinhBienBanBanGiao.Ngaylap=Convert.ToDateTime(gridViewBanGiao.GetFocusedRowCellDisplayText("NgayBanGiao"));
                SqlCommand cmd = new SqlCommand("tblChiTietBanGiao_DS", cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("mabienban", SqlDbType.Int)).Value = gridViewBanGiao.GetFocusedRowCellDisplayText("MaBangiao");
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                gridControlChiTietBanGiao.DataSource = dt;
            }
            catch
            {
            }
           
        }

        private void txtTim_Enter(object sender, EventArgs e)
        {
            if (txtTim.Text == "Nhập thông tin cần tìm")
            {
                txtTim.Text = "";
            }
        }

        private void txtTim_Leave(object sender, EventArgs e)
        {
            if (txtTim.Text == "" || txtTim.Text != "")
            {
                txtTim.Text = "Nhập thông tin cần tìm";
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            frmLapBienBanBanGiao frm = new frmLapBienBanBanGiao();
            frm.ShowDialog();
            loaddata();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa biên bản bàn giao hay không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SqlCommand cmd = new SqlCommand("tblBienBanBanGiao_Xoa",cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("mabangiao", SqlDbType.Int)).Value = txtMaBienBan.Text;
                if (cmd.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Xóa thông tin bàn giao thành công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    UcBienBanBanGiao_Load(sender, e);
                }
            }
        }

        private void gridViewBanGiao_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gridViewBanGiao.FocusedRowHandle)
            {
                e.Appearance.BackColor = Color.DeepSkyBlue;
            }
        }
      
        private void btnSuaBienBan_Click(object sender, EventArgs e)
        {
            bientinhBienBanBanGiao.Mabienban = int.Parse(gridViewBanGiao.GetFocusedRowCellDisplayText("MaBangiao").ToString());
            bientinhBienBanBanGiao.Ngaylap = Convert.ToDateTime(gridViewBanGiao.GetFocusedRowCellDisplayText("NgayBanGiao"));
            bientinhBienBanBanGiao.Togiao = int.Parse(gridViewBanGiao.GetRowCellValue(gridViewBanGiao.FocusedRowHandle, "ToGiao").ToString());
            bientinhBienBanBanGiao.Tonhan = int.Parse(gridViewBanGiao.GetRowCellValue(gridViewBanGiao.FocusedRowHandle, "ToNhan").ToString());
            bientinhBienBanBanGiao.Nguoigiao = gridViewBanGiao.GetRowCellValue(gridViewBanGiao.FocusedRowHandle, "NVGiao").ToString();
            bientinhBienBanBanGiao.Nguoinhan = gridViewBanGiao.GetRowCellValue(gridViewBanGiao.FocusedRowHandle, "NVNhan").ToString();
            vitri = gridViewBanGiao.FocusedRowHandle;
            frmSuaBienBanBanGiao frm = new frmSuaBienBanBanGiao();
            frm.ShowDialog();
            loaddata();
            gridViewBanGiao.FocusedRowHandle = vitri;

        }

        private void btnInBienBan_Click(object sender, EventArgs e)
        {
            frmInBienBanBanGiao frm = new frmInBienBanBanGiao();
            frm.ShowDialog();
            loaddata();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        

    }
}
